package com.toune.villager.adapter

import android.content.Context
import android.view.ViewGroup
import android.widget.ImageView
import androidx.annotation.NonNull
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.toune.villager.bean.BannerDataBean
import com.youth.banner.adapter.BannerAdapter


/**
 * 自定义布局，下面是常见的图片样式，更多实现可以看demo，可以自己随意发挥
 */
class MyBannerAdapter(context: Context, mDatas: List<BannerDataBean?>?) :
    BannerAdapter<BannerDataBean?, MyBannerAdapter.BannerViewHolder?>(mDatas) {

    //创建ViewHolder，可以用viewType这个字段来区分不同的ViewHolder
    override fun onCreateHolder(parent: ViewGroup, viewType: Int): BannerViewHolder {
        val imageView = ImageView(parent.getContext())
        //注意，必须设置为match_parent，这个是viewpager2强制要求的
        imageView.setLayoutParams(
            ViewGroup.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT
            )
        )
        imageView.setScaleType(ImageView.ScaleType.CENTER_CROP)
        return BannerViewHolder(imageView)
    }

    private val context = context

    inner class BannerViewHolder(@NonNull view: ImageView) :
        RecyclerView.ViewHolder(view) {
        var imageView: ImageView

        init {
            imageView = view
        }
    }

    override fun onBindView(
        holder: BannerViewHolder?,
        data: BannerDataBean?,
        position: Int,
        size: Int
    ) {
        Glide.with(context).load(data!!.imgUrl).into(holder!!.imageView)
    }
}

